package com.zwr.sheller.shop.service.impl;

import com.zwr.sheller.core.common.WQQBusinessException;
import com.zwr.sheller.core.common.WQQExceptionCode;
import com.zwr.sheller.core.pojo.Seller;
import com.zwr.sheller.core.pojo.User;
import com.zwr.sheller.core.util.JsonUtil;
import com.zwr.sheller.shop.mapper.SellerMapper;
import com.zwr.sheller.shop.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author: 18032
 * @Date: 9:58 2022/1/7
 */
@Service
public class LoginServiceImpl implements LoginService {

    /**
     *设置token前缀(尽量复杂),防止背抓包
     */
    public static final String LOGIN = "WOSHIYIGETOKEN!667632@";
    private static final String LOGIN_SMS="login_sms:";
    @Autowired(required = false)
    SellerMapper sellerMapper;
    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    @Override
    public Seller queryUserByToken(String token) throws WQQBusinessException {
        String key = LOGIN+token;
        Object value = redisTemplate.opsForValue().get(key);
        if (StringUtils.isEmpty(value)){
            throw new WQQBusinessException(WQQExceptionCode.NO_TOKEN);
        }

        //延长用户token的有效时间,让用户有充足的时间完成关键业务,下单/支付
        redisTemplate.expire(key,30,TimeUnit.MINUTES);
        //获取token对应的值(user对象)
        return JsonUtil.stringToObject(value.toString(), Seller.class);
    }

    @Override
    public Seller login(String phone, String pwd) throws WQQBusinessException {
        Example example = new Example(Seller.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("sellerPhone", phone).andEqualTo("sellerPwd",pwd);
        List<Seller> sellers = sellerMapper.selectByExample(example);
        if (sellers.size()>1){
            throw new WQQBusinessException(WQQExceptionCode.UNKNOWN_EXCEPTION);
        }else if (sellers.size()==0){
            throw new WQQBusinessException(WQQExceptionCode.WRONG_PHONE_OR_PWD);
        }
        System.out.println(sellers.get(0)+"!!!!!");
        if (!StringUtils.isEmpty(sellers.get(0))){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = simpleDateFormat.format(new Date(System.currentTimeMillis()));
            sellers.get(0).setSellerLoginTime(format);
            int i = sellerMapper.updateByExampleSelective(sellers.get(0), example);
            System.out.println("i:"+i);
            return sellers.get(0);
        }
        throw new WQQBusinessException(WQQExceptionCode.WRONG_PHONE_OR_PWD);
    }

    @Override
    public Integer forgetPwd(String phone, String code, String pwd) throws WQQBusinessException {
        //验证code是否正确
        Object o = redisTemplate.opsForValue().get(LOGIN_SMS + phone);
        if (StringUtils.isEmpty(o) || !o.equals(code)){
            throw new WQQBusinessException(WQQExceptionCode.WRONG_CODE);
        }
        Seller seller = new Seller();
        seller.setSellerPhone(phone);
        Seller seller1 = sellerMapper.select(seller).get(0);
        if (StringUtils.isEmpty(seller1)){
            throw new WQQBusinessException(WQQExceptionCode.NO_USER);
        }
        //修改密码
        seller.setSellerPwd(pwd);
        Example example = new Example(Seller.class);
        example.createCriteria().andEqualTo("sellerPhone",phone);
        int i = sellerMapper.updateByExampleSelective(seller,example);
        if (i == 0){
            throw new WQQBusinessException(WQQExceptionCode.UPDATE_FAILED);
        }
        return i;
    }
}
